package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Type;
import com.qf.service.TypeService;
import com.qf.service.impl.TypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 29125
 * @version V1.0
 * @Project java2203
 * @Package com.qf.controller
 * @Description: 商品分类
 * @Date 2022/5/24 9:51
 */
@WebServlet("/type")
public class TypeController extends BaseServlet{

    //查询所有商品分类信息展示
    public String findAllType(HttpServletRequest request,HttpServletResponse response){
        //调用service业务层，查询所有商品分类数据
        //创建service层和dao层
        TypeService typeService=new TypeServiceImpl();
        List<Type> list= typeService.findAllType();

        //把数据存入request域中，携带到页面showGoodsType.jsp中展示出来，54行${goodsTypeList}
        request.setAttribute("goodTypeList",list);

        //携带数据到新页面，转发操作forward
        return "forward:/admin/showGoodsType.jsp";
    }

    //后台添加商品中的下拉框需要的商品分类数据
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //调用service业务层，查询所有商品分类数据
        //创建service层和dao层
        TypeService typeService=new TypeServiceImpl();
        List<Type> list= typeService.findAllType();

        //将商品分类集合对象数据转换成json格式字符串
        String jsonStri = JSON.toJSONString(list);

        //通过流将json格式字符串写回到原页面中
        response.getWriter().println(jsonStri);
    }
}
